
#pragma once

#include "stdafx.h"
#include "PointerArray.h"
#include "VarArray.cpp"

template <typename POINTER_TYPE>
CPointerArray<POINTER_TYPE>::CPointerArray(CLEAR_METHOD m)
{
	method = m; m_userDefine = nullptr;
}

template <typename POINTER_TYPE>
CPointerArray<POINTER_TYPE>::~CPointerArray()
{
	selfClear();
}

template <typename POINTER_TYPE>
void CPointerArray<POINTER_TYPE>::clear()
{
	selfClear();
	CVarArray<POINTER_TYPE>::clear();
}

template <typename POINTER_TYPE>
void CPointerArray<POINTER_TYPE>::selfClear()
{
	switch (method)
	{
	case Delete:
		if (recs) for (intptr_t i = 0; i < count; i++) if (recs[i]) delete recs[i];
		break;
	case Free:
		if (recs) for (intptr_t i = 0; i < count; i++) if (recs[i]) HeapFree(GetProcessHeap(), 0, (LPVOID)recs[i]);
		break;
	case UserDefine:
		if (m_userDefine && recs) for (intptr_t i = 0; i < count; i++) if (recs[i]) m_userDefine->userDefineDelete(recs[i]);
		break;
	}
}

template <typename POINTER_TYPE>
bool CPointerArray<POINTER_TYPE>::del(intptr_t nIndex)
{
	if (!recs || nIndex < 0 || nIndex >= count) return false;
	if (recs[nIndex]) switch (method)
	{
	case Delete: delete recs[nIndex]; break;
	case Free: HeapFree(GetProcessHeap(), 0, (LPVOID)recs[nIndex]); break;
	case UserDefine: if (m_userDefine) m_userDefine->userDefineDelete(recs[nIndex]); break;
	}
	return CVarArray<POINTER_TYPE>::del(nIndex);
}
